#include "Thread.h"

using namespace tp;

Thread::Thread(const Func& cb)
    :_isRunning(false)
     ,_cb(cb)
{}

Thread::~Thread()
{
    if (_isRunning)
    {
        pthread_detach(_pthID);
    }
}

void Thread::start()
{
    _isRunning = true;
    pthread_create(&_pthID, NULL, threadFunc, this);
}

void Thread::join()
{
    pthread_join(_pthID, NULL);
    _isRunning = false;
}

void* Thread::threadFunc(void* pArgs)
{
    Thread* pth = static_cast<Thread*>(pArgs);

    if (pth != NULL)
    {
        if (pth->_cb)
        {
            pth->_cb();
        }
    }

    return NULL;
}
